﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using RM.WinClient.Utilities;
using System.Windows.Threading;
using RM.WinClient.RMServices;

namespace RM.WinClient
{
    /// <summary>
    /// Interaction logic for QuestionDetails.xaml
    /// </summary>
    public partial class QuestionDetails : Page
    {
        Question gblQuestion = null;
        public QuestionDetails()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(QuestionDetails_Loaded);
        }

        public QuestionDetails(Question objQuestion)
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(QuestionDetails_Loaded);
            gblQuestion = objQuestion;
        }

        void QuestionDetails_Loaded(object sender, RoutedEventArgs e)
        {
            lstVwQuestions.ItemsSource = gblQuestion.QuestionList;
            TestTimer.OnTick += new TestTimer.Ticks(TestTimer_OnTick);
        }

        void TestTimer_OnTick()
        {
            Dispatcher.BeginInvoke(DispatcherPriority.Normal, (EventHandler)
             delegate
             {
                 txtDuration.Text = Util.FormatTime(TestTimer.RunningTime);
             }, null, null);
        }

        private void lstVwQuestions_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void btnBack_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.GoBack();
        }

        private void btnStartTest_Click(object sender, RoutedEventArgs e)
        {
            string btntext = btnStartTest.Content.ToString();
            btnStartTest.Content = btntext == "Pause" ? "Resume" : "Pause";
            switch (btntext)
            {
                case "Start":
                    TestTimer.Start();
                    btnStop.Visibility = Visibility.Visible;
                    TestTimer.OnStop += new TestTimer.StopTest(TestTimer_OnStop);
                    break;
                case "Pause":
                    TestTimer.Pause();
                    Util.LogMessage(new OperationLog() { Entity = "Test", Data = ((Candidate)App.Current.Resources[Constants.SelectedCandidate]).Id.ToString(), Message = "Test Paused for candidate ID" + ((Candidate)App.Current.Resources[Constants.SelectedCandidate]).Id + ";" });
                    break;
                case "Resume":
                    TestTimer.Resume();
                    Util.LogMessage(new OperationLog() { Entity = "Test", Data = ((Candidate)App.Current.Resources[Constants.SelectedCandidate]).Id.ToString(), Message = "Test Resumed for candidate ID" + ((Candidate)App.Current.Resources[Constants.SelectedCandidate]).Id + ";" });
                    break;
            }
        }

        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBoxResult.Yes == MessageBox.Show("Are you sure you want to stop the test?", "Stop", MessageBoxButton.YesNo))
            {
                TestTimer.OnStop += new TestTimer.StopTest(TestTimer_OnStop);
                TestTimer.Stop();
                Util.LogMessage(new OperationLog() { Entity = "Test", Data = ((Candidate)App.Current.Resources[Constants.SelectedCandidate]).Id.ToString(), Message = "Test Completed for candidate ID" + ((Candidate)App.Current.Resources[Constants.SelectedCandidate]).Id + ";" });
            }
        }

        void TestTimer_OnStop()
        {
            if (App.Current.Resources.Contains(Constants.SelectedCandidate))
            {
                Candidate objCandidate = (Candidate)App.Current.Resources[Constants.SelectedCandidate];
                Test objtest = new Test();
                objtest.Candidate = objCandidate;
                objtest.Duration = TestTimer.RunningTime;
                objtest.StartDateTime = TestTimer.StartDateTime;
                objtest.EndDateTime = TestTimer.EndDateTime;
                objtest.Question = gblQuestion;
                NavigationService.Navigate(new Finish(objtest));
            }
        }

    }
}
